package com.moyun.model.po.system;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.moyun.common.validation.SysUserModifyGroup;
import com.moyun.model.common.CommonFieldEntity;
import com.moyun.model.po.org.SysOrgRoleEntity;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.validation.constraints.NotEmpty;
import java.io.Serializable;
import java.sql.Blob;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表
 * </p>
 *
 * @author yangboxuan
 * @since 2021-07-01
 */

@Data
@Slf4j
@Accessors(chain = true)
@TableName("sys_user")
@JsonIgnoreProperties({"userPassword"})
public class SysUserEntity extends CommonFieldEntity implements UserDetails, Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 用户ID
     */
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    @NotEmpty(groups = {SysUserModifyGroup.class},message = "用户id不能为空")
    private String id;

    /**
     * 用户名
     */
    @TableField("user_name")
    @NotEmpty(message = "登录用户名不能为空")
    private String userName;

    /**
     * 密码
     */
    @TableField("user_password")
    private String userPassword;

    /**
     * 邮箱
     */
    @TableField("user_email")
    private String userEmail;

    /**
     * 简介
     */
    @TableField("user_info")
    private String userInfo;

    /**
     * 头像
     */
    @TableField("head_img")
    private Blob headImg;

    /**
     * 用户是否可用
     */
    @TableField("enable")
    private Integer enable;

//    /**
//     * 用户的角色集合 (使用数据库中 sys_role 系统角色表)
//     */
//    @TableField(exist = false)
//    private List<SysRoleEntity> roleList;
//
//
//    @Override
//    public Collection<? extends GrantedAuthority> getAuthorities() {
//        // 防止 在 sysUserEntityController 控制层返回用户信息 json序列化时报错 对roleList判空
//        if (roleList==null) {
//            return null;
//        }
//        List<SimpleGrantedAuthority> authorities = roleList.stream().map(r -> new SimpleGrantedAuthority(r.getRoleName())).collect(Collectors.toList());
//        log.info("为{}用户赋予角色{}",this.userName,authorities);
//        return authorities;
//    }

    /**
     * 用户的角色集合 (使用数据库中 sys_org_role 系统组织角色表)
     */
    @TableField(exist = false)
    private List<SysOrgRoleEntity> roleList;


    @Override
//    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 防止 在 sysUserEntityController 控制层返回用户信息 json序列化时报错 对roleList判空
        if (roleList == null) {
            return null;
        }
        List<SimpleGrantedAuthority> authorities = roleList.stream().map(r -> new SimpleGrantedAuthority(r.getNumber())).collect(Collectors.toList());
        log.info("为{}用户赋予角色{}", this.userName, authorities);
        return authorities;
    }

    @JsonIgnore
    @Override
    public String getPassword() {
        return this.userPassword;
    }

    @JsonIgnore
    @Override
    public String getUsername() {
        return this.userName;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return this.enable == 0;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }

}
